<template>
  <el-dialog
    :title="!dataForm.did ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="医生姓名" prop="dname">
      <el-input v-model="dataForm.dname" placeholder="医生姓名"></el-input>
    </el-form-item>
    <el-form-item label="医生性别" prop="dsex">
      <el-input v-model="dataForm.dsex" placeholder="医生性别"></el-input>
    </el-form-item>
    <el-form-item label="医生年龄" prop="dage">
      <el-input v-model="dataForm.dage" placeholder="医生年龄"></el-input>
    </el-form-item>
    <el-form-item label="医生职称" prop="dzc">
      <el-input v-model="dataForm.dzc" placeholder="医生职称"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="dtel">
      <el-input v-model="dataForm.dtel" placeholder="电话号码"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
          did: 0,
          dname: '',
          dsex: '',
          dage: '',
          dzc: '',
          dtel: ''
        },
        dataRule: {
          dname: [
            { required: true, message: '医生姓名不能为空', trigger: 'blur' }
          ],
          dsex: [
            { required: true, message: '医生性别不能为空', trigger: 'blur' }
          ],
          dage: [
            { required: true, message: '医生年龄不能为空', trigger: 'blur' }
          ],
          dzc: [
            { required: true, message: '医生职称不能为空', trigger: 'blur' }
          ],
          dtel: [
            { required: true, message: '医生电话号码不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.did = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.did) {
            this.$http({
              url: this.$http.adornUrl(`/generator/doctor/info/${this.dataForm.did}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.dname = data.doctor.dname
                this.dataForm.dsex = data.doctor.dsex
                this.dataForm.dage = data.doctor.dage
                this.dataForm.dzc = data.doctor.dzc
                this.dataForm.dtel = data.doctor.dtel
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/generator/doctor/${!this.dataForm.did ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'did': this.dataForm.did || undefined,
                'dname': this.dataForm.dname,
                'dsex': this.dataForm.dsex,
                'dage': this.dataForm.dage,
                'dzc': this.dataForm.dzc,
                'dtel': this.dataForm.dtel
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
